package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

public class ShufeBbsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://bbs.shufe.org/bbs/a/a.asp\\?B=[0-9]*&ID=[0-9]*",
		};
		return RegexUtils.matchAny(url, regex);
	}

	public static HashMap<String, String> getLinks(String url, String html)
	{
		//http://bbs.shufe.org/bbs/b/b.asp?B=13
		//http://bbs.shufe.org/bbs/a/a.asp?B=13&ID=1420964
		HashMap<String, String> links = new HashMap<String, String>();
		try
		{
			String boardId = url.substring(url.lastIndexOf("=")+1);
			if(boardId==null)
			{
				return null;
			}
			String regex = "(?m)^leadbbs\\((.*\\));$";
			Matcher matcher = Pattern.compile(regex).matcher(html);
			while(matcher.find())
			{
//				System.err.println(matcher.group());
				String newUrl = "";
				String[] params = matcher.group(1).split(",");
				if(params.length>2)
				{
					newUrl = String.format("http://bbs.shufe.org/bbs/a/a.asp?B=%s&ID=%s", boardId, params[1]);
				}
				links.put(newUrl, "");
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return links;
	}
	
	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (!isDetailPage(urlMeta.getUrl())) {
		}
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		bbs.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		String author = null;

		Document doc = Jsoup.parse(htmltxt);

		Elements titleList = doc.select("font.HeadFont");
		if(titleList.size()>0)
		{
			title = titleList.get(0).text();
		}
		date = DateUtils.matchDate(doc.select("td:contains(发表:)").text());
		String regex = "ds\\(.*\\);";
		Matcher matcher = Pattern.compile(regex).matcher(htmltxt);
		if(matcher.find()){
			String[] params = matcher.group().split(",");
			if(params.length>6)
			{
				date = DateUtils.matchDate(params[3]);
				author = StringUtils.substringBetween(params[5], "\"",  "\"");
			}
		}
		Elements contentList = doc.select("table[width=100%]");
		if(contentList.size()>6)
		{
			content = HtmlCleaner.getContentHtml(url, contentList.get(7));
		}
		
		bbs.setTitle(title);
		bbs.setContent(content);
		bbs.setDate(date);
		bbs.setAuthor(author);
		/**
		 * 解析用于更新的地址
		 */
		bbs.setUpdateUrl(url);
		return bbs;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	public static void main(String[] args) {
		ShufeBbsAnalyse a = new ShufeBbsAnalyse();

		String url = "http://bbs.shufe.org/bbs/a/a.asp?B=3&ID=1450841";
//		UrlMeta urlMeta = CrawlHTML.responseToURL(url);
//		Map<String, String> map = ShufeBbsAnalyse.getLinks(urlMeta.getUrl(), urlMeta.getHtml());
//		System.out.println(map);
		System.out.println(UrlArgumentTop.FromatUrl(url));
		System.out.println(DoMainUtils.GetDomainName(url));
		if (!a.isDetailPage(url)) {
			System.out.println("不匹配规则");
		} else {
			UrlMeta meta = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = a.parserHtml(meta);
			System.out.println(parserHtml);
		}
	}
	

}
